package com.andnnl.replace;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by chenss on 2021/9/6.
 */
public class ReplaceRecordTest {
    public static void main(String[] args) throws Exception {
        String fileName="template.txt";
        Path path = Paths.get(fileName);

        Stream<String> rc= Files.lines(path, Charset.defaultCharset());
        List<String> list=rc.collect(Collectors.toList());
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("----------------------------------");
        System.out.println();
        int start=-1;
        String pre="";
//        String uid = "szbdyy";
//        int eprId = 567; 2244
//        String uid = "szplgskyy";
//        int eprId = 2244;
        String uid = "";
        int eprId = 0;
        String sql= "insert into yh_template (tempName ,ctx ,uid ,eprId ,updateTime ) values ('YYY' ,'XXX' ,'ZZZ_UID', ZZZ_EPRID, getdate());";
        StringBuffer sb=new StringBuffer();
        StringBuffer title=new StringBuffer();
        Pattern pattern = Pattern.compile("--(.+?)(\\d+$)");
        Pattern patternUser = Pattern.compile("----(.+?),(\\d+$)");
        for (String s : list) {
            if(StrUtil.isBlank(s))continue;
            if(s.startsWith("insert into"))continue;
            if(s.startsWith("----") && s.indexOf(",")!=-1){
                title.append(s).append("\n\n");
                List<String> allGroups = ReUtil.getAllGroups(patternUser, s);
                uid=allGroups.get(1);
                eprId=Integer.parseInt(allGroups.get(2));
                continue;
            }
            if(s.startsWith("--")){
                List<String> allGroups = ReUtil.getAllGroups(pattern, s);
                if(start>=0){
                    title.append("--").append(pre+start).append("\n\n");
                }
                pre = allGroups.get(1);
                start = Integer.parseInt(allGroups.get(2));
                continue;
            }
            String sxx=sql.replaceAll("YYY",pre+(++start));
            sxx=sxx.replaceAll("XXX", Matcher.quoteReplacement(s));
            sxx=sxx.replaceAll("ZZZ_UID",uid);
            sxx=sxx.replaceAll("ZZZ_EPRID",""+eprId);
            System.out.println(sxx);
            sb.append(sxx).append("\n");
        }
        title.append("--").append(pre+start).append("\n\n");

        String s="";
        String[] ss = s.split("\n");
        System.out.println("----------------------------------");
        System.out.println();
        System.out.println(title.toString());
//
//        for (String str : ss) {
//            if(StrUtil.isBlank(str))continue;
//            String sxx=sql.replaceAll("YYY","深圳交警-业务"+(++start));
//            sxx=sxx.replaceAll("XXX",str);
//            System.out.println(sxx);
//        }
        Files.write(path, title.toString().getBytes());
    }
}
